package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author Mamba
 * @version 1.0
 * @Description
 * @date 2021/1/4 21:07
 */
@Controller
public class CartController {
    @Autowired
    private CartFeignClient cartFeignClient;

    //加入购物车
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
      cartFeignClient.addToCart(skuId,skuNum);
      //Model底层 Request   Request.setAttribute(k,v)不行，不能这样使用
        redirectAttributes.addAttribute("skuId",skuId);
        redirectAttributes.addAttribute("skuNum",skuNum);
      //加入成功页面
        return "redirect:http://cart.gmall.com/addToCart.html";
    }

    //去显示购物成功页面
    @GetMapping("/addToCart.html")
    public String addToCart(Long skuId,Integer skuNum,Model model){
        CartInfo cartInfo = cartFeignClient.findCartInfoBySkuId(skuId);
        cartInfo.setSkuNum(skuNum);
        model.addAttribute("cartInfo",cartInfo);
        //加入成功页面
        return "cart/addCart";
    }
    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){
        return "cart/index";
    }
}
